package com.rfid.afd.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.rfid.rls_psw.R;
import com.rfid.util.RfidManager;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScanStratigraphyActivity extends Activity implements RfidManager.RfidListener{

    @BindView(R.id.scan_please)
    TextView mTextView;

    private String mRfid;

    public static void startActivity(Context context,Class clazz) {
        Intent intent = new Intent();
        intent.setClass(context,ScanStratigraphyActivity.class);
        intent.putExtra("class",clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_layout);
        ButterKnife.bind(this);
        RfidManager.getInstance().setDecodeEpcEnable(false);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        RfidManager.getInstance().stop();
        RfidManager.getInstance().setRfidListener(null);
        RfidManager.getInstance().setDecodeEpcEnable(false);
    }

    public void button(View v) {
        String edit = mTextView.getText().toString();
        if(TextUtils.isEmpty(edit)) {
           return;
        }
        mRfid = edit;
        RfidManager.getInstance().stop();
        RfidManager.getInstance().setRfidListener(null);
        ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this,mTextView, "stratigraphy");
        Intent intent = new Intent();
        intent.setClass(this, (Class) getIntent().getSerializableExtra("class"));
        intent.putExtra("shelf","SK02B0201");
        startActivity(intent,options.toBundle());
    }

    boolean scan;
    public void start(View v) {
        TextView textView = (TextView) v;
        if(!scan) {
            RfidManager.getInstance().setRfidListener(this);
            RfidManager.getInstance().start();
            textView.setText("停止扫描");
        } else {
            RfidManager.getInstance().setRfidListener(null);
            RfidManager.getInstance().stop();
            textView.setText("开始扫描");
        }
        scan = !scan;
    }

    @Override
    public void onRfid(String rfid) {
        mRfid = rfid;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                System.out.println(mRfid);
                mTextView.setText(rfid);
            }
        });
    }
}
